<template>
  <a-card :bordered='false'>
    <search ref='search' :config='searchConfig' @search='search' @getHeight='getHeight' />
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        :border='true'
        :resizable='true'
        :showOverflow='true'
        size='mini'
        :loading='loading'
        :height='pageHeight-seachHeight-90'
        :align='left'
        :columns='columns'
        :data='dataSource'
        :toolbar-config='tableToolbar'
       :checkbox-config="checkboxConfig"
        v-bind='vexConfig'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <div class='table-operator'>
            <a-button @click='handleAdd' type='primary'>新增</a-button>
            <a-dropdown v-has="'quartzJob:delete'" v-if='selectedRowKeys.length > 0'>
              <a-menu slot='overlay'>
                <a-menu-item key='1' @click='batchDel'>
                  <a-icon type='delete' />
                  删除
                </a-menu-item>
              </a-menu>
              <a-button style='margin-left: 8px'> 批量操作
                <a-icon type='down' />
              </a-button>
            </a-dropdown>
          </div>
        </template>
        <template #status='{ row }'>
          <a-tag v-if='row.status===1' color='green'>已启动</a-tag>
          <a-tag v-if='row.status===0' color='orange'>已暂停</a-tag>
        </template>
        <template #action='{ row:record }'>
          <a @click='resumeJob(record)' v-if='record.status==0'>启动</a>
          <a @click='pauseJob(record)' v-if='record.status==1'>停止</a>
          <a-divider type='vertical' v-if='record.status==1||record.status==0' />
          <a-dropdown>
            <a class='ant-dropdown-link'>更多
              <a-icon type='down' />
            </a>
            <a-menu slot='overlay'>
              <a-menu-item><a @click='executeImmediately(record)'>立即执行</a></a-menu-item>
              <a-menu-item><a @click='handleEdit(record)'>编辑</a></a-menu-item>
              <a-menu-item><a @click='expenseDetails(record)'>执行结果</a></a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>

        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync='ipagination.current'
            :page-size.sync='ipagination.pageSize'
            :total='ipagination.total'
            :pageSizes='ipagination.pageSizeOptions'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <policys ref="policysModal" v-bind="policysData"></policys>
    <quartzJob-modal ref='modalForm' @ok='modalFormOk'></quartzJob-modal>
  </a-card>
</template>

<script>
import policys from '@comp/cn-components/Policys'
import QuartzJobModal from '@page-modules/QuartzJobModal'
import { getAction } from '@/api/manage'
import { PageListMixin } from '@/mixins/PageListMixin'
import JEllipsis from '@/components/cn-form/JEllipsis'

export default {
  name: 'QuartzJobList',
  mixins: [PageListMixin],
  components: {
    QuartzJobModal,
    JEllipsis,
    policys
  },
  data() {
    return {
      description: '定时任务',
      searchConfig: [
        {
          type: 'input',
          name: 'jobClassName',
          placeholder: '任务类名'
        },
        {
          type: 'a-select',
          name: 'status',
          placeholder: '任务状态',
          option: [
            {
              name: '正常',
              value: 1
            }, {
              name: '停止',
              value: 2
            }
          ]
        }
      ],
      // 查询条件
      queryParam: {},
      // 表头
      columns: [
        { type: 'checkbox', width: 50, fixed: 'left' },

        {
          title: '任务类名',
          minWidth: 80,
          field: 'jobClassName'
        },
        {
          title: 'cron表达式',
          minWidth: 80,
          field: 'cronExpression'
        },
        {
          title: '参数',
          minWidth: 80,
          field: 'parameter'
        },
        {
          title: '描述',
          minWidth: 80,
          field: 'description'
        },
        {
          title: '状态',
          minWidth: 100,
          field: 'status',
          slots: { default: 'status' },
          filterMultiple: false,
          filters: [
            { label: '已启动', value: 1 },
            { label: '已暂停', value: 0 }
          ]
        },
        {
          title: '创建人',
          field: 'createName',
          minWidth: 80,
        },
        {
          title: '创建时间',
          field: 'createTime',
          minWidth: 80,
        },
        {
          title: '更新人',
          field: 'updateName',
          minWidth: 80,
        },
        {
          title: '更新时间',
          field: 'updateTime',
          minWidth: 80,
        },
        {
          title: '操作',
          field: 'action',
          minWidth:100,
          width: 180,
          slots: { default: 'action' }
        }
      ],

      url: {
        list: window._CONFIG['domianJobURL'] + '/sys/quartzJob/list',
        delete: window._CONFIG['domianJobURL'] + '/sys/quartzJob/delete',
        deleteBatch: window._CONFIG['domianJobURL'] + '/sys/quartzJob/deleteBatch',
        pause: window._CONFIG['domianJobURL'] + '/sys/quartzJob/pause',
        resume: window._CONFIG['domianJobURL'] + '/sys/quartzJob/resume',
        exportXlsUrl: window._CONFIG['domianJobURL'] + 'sys/quartzJob/exportXls',
        importExcelUrl: window._CONFIG['domianJobURL'] + '/sys/quartzJob/importExcel',
        execute: window._CONFIG['domianJobURL'] + '/sys/quartzJob/execute'
      }
    }
  },
  computed: {
    policysData(row) {
      return {
        title: '执行结果',
        width: `${50}%`,
        rowKey: 'id',
        tableShowSelect:false,
        way: 'post',
        tableUrl: window._CONFIG['domianJobURL'] + '/sys/quartzJobLog/list ',
        tableColums: [
          {
            title: '任务执行状态',
            align: 'center',
            dataIndex: 'jobStatus',
            customRender: function(t, r, index) {
              return t==1?'成功':'失败'
            }
          },
          {
            title: '执行结果',
            align: 'center',
            dataIndex: 'jobMsg'
          },
          {
            title: '开始时间',
            align: 'center',
            dataIndex: 'startDate'
          },
          {
            title: '结束时间',
            align: 'center',
            dataIndex: 'endDate',
          },
        ],
        haveSelect:{},
        multiple: false
      }
    },
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },

  methods: {

    //筛选需要重写handleTableChange
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
      }
      //这种筛选方式只支持单选
      this.filters.status = filters.status[0]
      this.ipagination = pagination
      this.loadData()
    },
    pauseJob: function(record) {
      var that = this
      //暂停定时任务
      this.$confirm({
        title: '确认暂停',
        content: '是否暂停选中任务?',
        onOk: function() {
          getAction(that.url.pause, { jobClassName: record.jobClassName }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })

    },
    resumeJob: function(record) {
      var that = this
      //恢复定时任务
      this.$confirm({
        title: '确认启动',
        content: '是否启动选中任务?',
        onOk: function() {
          getAction(that.url.resume, { jobClassName: record.jobClassName }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
    expenseDetails(cord){
      let params = {
        jobId:cord.id
      }
      this.$refs.policysModal.edit(params)
    },
    executeImmediately(record) {
      var that = this
      //立即执行定时任务
      this.$confirm({
        title: '确认提示',
        content: '是否立即执行任务?',
        onOk: function() {
          getAction(that.url.execute, { id: record.id }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>
